<?php

class Categories extends Eloquent {
    protected $table = 'categories';
    //select to data
    public static function Select($shop_id)
    {
        try{

            $query_select="SELECT categories.*  FROM categories INNER JOIN industries ON 
categories.industry_id = industries.id WHERE industries.shop_id=$shop_id ";
            
            return DB::select(DB::raw($query_select));
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    public static function getCategoryId($name)
    {
        try
        {
            $query_category_id=self::where('name','=',$name)
                                ->select('id')->first();

            //var_dump($query_shop_id); exit;
            if($query_category_id)
            {
                return $query_category_id->id;
            }
            return -1;
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    //insert to data
     public static function Insert($name,$description,$industry_id)
    {
        try{
                $categories= new Categories();
                $categories->name = $name;
                $categories->description =$description;
                $categories->industry_id=$industry_id;
                $categories->save();
                $category_id = $categories->id;
                return $category_id;
            }
        catch(Exception $e)
        {
            throw $e;
        }  
    } 
    //Edit to data      
    public static function Edit($categories_id,$name,$description,$industry_id)
    {
        try{

            $query_edit=Categories::where('id',$categories_id)->update(array(
                                                        'id' => $categories_id,
                                                        'name' =>$name,
                                                        'description'=>$description,
                                                        'industry_id'=>$industry_id
                                                        
                                                    ));
            return $query_edit;
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    //Delete to data 
    public static function Deletedata($categories_id){
        try{
            $query_delete ="DELETE FROM categories WHERE id = '$categories_id'";
            $delete=DB::delete($query_delete);
            return true;
        }
        catch(Exception $e){
            throw $e;
        }
    }
}
